<?php
namespace app\middleware;

class RequestLimit {
    public function handle($request, \Closure $next)
    {
        $limitKey = 'request_limit_'.$request->ip();
        if($request->user){
            $limitKey = 'request_limit_'.$request->user->id;
        }
        $times = cache($limitKey);
        if(!$times){
            $times = 0;
        }
        if($times > 100){
            return response(['code' => 0, 'msg'=>'您的操作过于频繁'],200, [], 'json');
        }
        cache($limitKey, $times+1, 10);
        return $next($request);
    }
}